import {create, type StateCreator} from "zustand";
import type {UserInfo} from "../request/user.ts";
import {createJSONStorage, persist} from "zustand/middleware";

interface State {
    userInfo: UserInfo | object,
    setUserInfo: (userInfo: UserInfo) => void
}

const storeCreator: StateCreator<
    State,
    [["zustand/persist", unknown]]
> = (set) => ({
    userInfo: {},
    setUserInfo: (userInfo) => set(state => ({userInfo: {...state.userInfo, ...userInfo}})),
});

export const useUserState = create<State, [["zustand/persist", unknown]]>(
    persist(
        storeCreator,
        {
            name: 'userStore',
            storage: createJSONStorage(() => localStorage)
        },
    )
)

